package com.servicio.common.UI;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.border.MatteBorder;

import net.miginfocom.swing.MigLayout;

import org.hibernate.Query;
import org.hibernate.Session;

import com.servicio.common.beans.Cities;
import com.servicio.common.beans.MedicalEntity;
import com.servicio.common.data.HibernateUtil;

public class NewPatient extends JInternalFrame {
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField textField_9;

	private JComboBox cboCities = new JComboBox();
	private JComboBox cboOS1 = new JComboBox();
	private JComboBox cboOS2 = new JComboBox();

	private Vector<String> allCities;
	private Vector<String> allMedicalEntities;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					NewPatient frame = new NewPatient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public NewPatient() {
		setBackground(new Color(230, 230, 250));

		SwingWorker queryWorker = new SwingWorker<Vector<String>, Void>() {

			@Override
			protected Vector<String> doInBackground() throws Exception {
				Vector<String> tmp = new Vector<String>();
				Session session = HibernateUtil.getSessionFactory()
						.openSession();
				session.beginTransaction();
				Query qCities = session.getNamedQuery("AllCities");
				List resultList = qCities.list();
				// Vector<Vector> results = new Vector<Vector>();
				for (Object obj : resultList) {
					// System.out.println(obj);
					Cities c = (Cities) obj;
					// cboCities.addItem(c.getcNombre());
					tmp.add(c.getcNombre());
				}

				return tmp;
			}

			protected void done() {
				try {
					allCities = get();
					for (String s : allCities)
						cboCities.addItem(s);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};

		SwingWorker queryWorker2 = new SwingWorker<Vector<String>, Void>() {

			@Override
			protected Vector<String> doInBackground() throws Exception {
				Vector<String> tmp = new Vector<String>();
				Session session = HibernateUtil.getSessionFactory()
						.openSession();
				session.beginTransaction();
				Query qMedicalEntities = session
						.getNamedQuery("AllMedicalEntities");
				List resultList2 = qMedicalEntities.list();

				// Vector<Vector> results = new Vector<Vector>();
				for (Object res : resultList2) {
					// System.out.println(obj);
					MedicalEntity me = (MedicalEntity) res;
					tmp.add(me.geteCodigo().toString() + " | "
							+ me.geteDescrip());
				}
				return tmp;
			}

			protected void done() {
				try {
					allMedicalEntities = get();
					for (String s : allMedicalEntities) {
						cboOS1.addItem(s);
						cboOS2.addItem(s);
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		};

		queryWorker.execute();
		queryWorker2.execute();

		setIconifiable(true);
		getContentPane().setForeground(Color.WHITE);
		setForeground(Color.BLACK);
		setBorder(new MatteBorder(1, 1, 1, 1, (Color) Color.GRAY));
		setClosable(true);
		setTitle("Nuevo Paciente");
		setBounds(100, 100, 474, 274);
		getContentPane().setLayout(
				new MigLayout("", "[][grow][][][grow]", "[][][][][][][][][][][][]"));
		
		JLabel lblNewLabel_11 = new JLabel("Ingreso de Paciente");
		lblNewLabel_11.setFont(new Font("Arial", Font.BOLD, 16));
		lblNewLabel_11.setForeground(new Color(51, 102, 153));
		lblNewLabel_11.setBackground(new Color(153, 204, 204));
		getContentPane().add(lblNewLabel_11, "cell 0 0 2 1");

		JLabel lblNewLabel = new JLabel("Numero");
		lblNewLabel.setFont(new Font("Arial", Font.PLAIN, 12));
		lblNewLabel.setForeground(Color.DARK_GRAY);
		getContentPane().add(lblNewLabel, "cell 0 1,alignx trailing");

		textField = new JTextField();
		textField.setFont(new Font("Arial", Font.BOLD, 12));
		textField.setForeground(Color.BLACK);
		getContentPane().add(textField, "cell 1 1,growx");
		textField.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("Apellido");
		lblNewLabel_1.setFont(new Font("Arial", Font.PLAIN, 12));
		lblNewLabel_1.setForeground(Color.DARK_GRAY);
		getContentPane().add(lblNewLabel_1, "cell 0 2,alignx trailing");

		textField_1 = new JTextField();
		textField_1.setFont(new Font("Arial", Font.BOLD, 12));
		textField_1.setForeground(Color.BLACK);
		getContentPane().add(textField_1, "cell 1 2,growx");
		textField_1.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("Nombre");
		lblNewLabel_2.setFont(new Font("Arial", Font.PLAIN, 12));
		lblNewLabel_2.setForeground(Color.DARK_GRAY);
		getContentPane().add(lblNewLabel_2, "cell 2 2,alignx trailing");

		textField_2 = new JTextField();
		textField_2.setFont(new Font("Arial", Font.BOLD, 12));
		textField_2.setForeground(Color.BLACK);
		getContentPane().add(textField_2, "cell 3 2 2 1,growx");
		textField_2.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("Domicilio");
		lblNewLabel_3.setFont(new Font("Arial", Font.PLAIN, 12));
		lblNewLabel_3.setForeground(Color.DARK_GRAY);
		getContentPane().add(lblNewLabel_3, "cell 0 3");

		textField_3 = new JTextField();
		textField_3.setFont(new Font("Arial", Font.BOLD, 12));
		textField_3.setForeground(Color.BLACK);
		getContentPane().add(textField_3, "cell 1 3,growx");
		textField_3.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("D.N.I.");
		lblNewLabel_4.setFont(new Font("Arial", Font.PLAIN, 12));
		lblNewLabel_4.setForeground(Color.DARK_GRAY);
		getContentPane().add(lblNewLabel_4, "cell 2 3,alignx trailing");

		textField_4 = new JTextField();
		textField_4.setFont(new Font("Arial", Font.BOLD, 12));
		textField_4.setForeground(Color.BLACK);
		getContentPane().add(textField_4, "cell 3 3 2 1,growx");
		textField_4.setColumns(10);

		JLabel lblNewLabel_5 = new JLabel("Localidad");
		lblNewLabel_5.setFont(new Font("Arial", Font.PLAIN, 12));
		lblNewLabel_5.setForeground(Color.DARK_GRAY);
		getContentPane().add(lblNewLabel_5, "cell 0 4,alignx trailing");

		cboCities.setFont(new Font("Arial", Font.BOLD, 12));
		cboCities.setForeground(Color.BLACK);
		getContentPane().add(cboCities, "cell 1 4,growx");

		JLabel lblNewLabel_6 = new JLabel("Fec. Nacimiento");
		lblNewLabel_6.setFont(new Font("Arial", Font.PLAIN, 12));
		lblNewLabel_6.setForeground(Color.DARK_GRAY);
		getContentPane().add(lblNewLabel_6, "cell 2 4,alignx trailing");

		textField_5 = new JTextField();
		textField_5.setFont(new Font("Arial", Font.BOLD, 12));
		textField_5.setForeground(Color.BLACK);
		getContentPane().add(textField_5, "cell 3 4 2 1,growx");
		textField_5.setColumns(10);

		JLabel lblNewLabel_7 = new JLabel("Tel\u00E9fono");
		lblNewLabel_7.setFont(new Font("Arial", Font.PLAIN, 12));
		lblNewLabel_7.setForeground(Color.DARK_GRAY);
		getContentPane().add(lblNewLabel_7, "cell 0 5,alignx trailing");

		textField_6 = new JTextField();
		textField_6.setFont(new Font("Arial", Font.BOLD, 12));
		textField_6.setForeground(Color.BLACK);
		getContentPane().add(textField_6, "cell 1 5,growx");
		textField_6.setColumns(10);

		JLabel lblNewLabel_8 = new JLabel("Edad");
		lblNewLabel_8.setFont(new Font("Arial", Font.PLAIN, 12));
		lblNewLabel_8.setForeground(Color.DARK_GRAY);
		getContentPane().add(lblNewLabel_8, "cell 2 5,alignx trailing");

		textField_7 = new JTextField();
		textField_7.setFont(new Font("Arial", Font.BOLD, 12));
		textField_7.setForeground(Color.BLACK);
		getContentPane().add(textField_7, "cell 3 5 2 1,growx");
		textField_7.setColumns(10);

		JSeparator separator = new JSeparator();
		getContentPane().add(separator, "cell 0 6");

		JLabel lblNewLabel_9 = new JLabel("O.S. 1");
		lblNewLabel_9.setFont(new Font("Arial", Font.PLAIN, 12));
		lblNewLabel_9.setForeground(Color.DARK_GRAY);
		getContentPane().add(lblNewLabel_9, "cell 0 8,alignx trailing");

		cboOS1.setFont(new Font("Arial", Font.BOLD, 12));
		cboOS1.setForeground(Color.BLACK);
		getContentPane().add(cboOS1, "cell 1 8 2 1,growx");

		textField_8 = new JTextField();
		textField_8.setFont(new Font("Arial", Font.BOLD, 12));
		textField_8.setForeground(Color.BLACK);
		getContentPane().add(textField_8, "cell 3 8 2 1,growx");
		textField_8.setColumns(10);

		JLabel lblNewLabel_10 = new JLabel("O.S. 2");
		lblNewLabel_10.setForeground(Color.DARK_GRAY);
		lblNewLabel_10.setFont(new Font("Arial", Font.PLAIN, 12));
		getContentPane().add(lblNewLabel_10, "cell 0 9,alignx trailing");

		cboOS2.setFont(new Font("Arial", Font.BOLD, 12));
		cboOS2.setForeground(Color.BLACK);
		getContentPane().add(cboOS2, "cell 1 9 2 1,growx");

		textField_9 = new JTextField();
		textField_9.setFont(new Font("Arial", Font.BOLD, 12));
		textField_9.setForeground(Color.BLACK);
		getContentPane().add(textField_9, "cell 3 9 2 1,growx");
		textField_9.setColumns(10);

		JButton btnNewButton_1 = new JButton("Revertir");
		getContentPane().add(btnNewButton_1, "cell 1 11,alignx right");

		JButton btnCancelar = new JButton("Cancelar");
		getContentPane().add(btnCancelar, "cell 2 11,alignx right");

		JButton btnNewButton = new JButton("Guardar");
		btnNewButton.setFont(new Font("Arial", Font.BOLD, 12));
		getContentPane().add(btnNewButton, "cell 4 11,alignx left");

		/*
		 * session.beginTransaction(); Query qMedicalEntities =
		 * session.getNamedQuery("AllMedicalEntities"); List resultList2 =
		 * qMedicalEntities.list();
		 * 
		 * // Vector<Vector> results = new Vector<Vector>(); for (Object res :
		 * resultList2) { // System.out.println(obj); MedicalEntity me =
		 * (MedicalEntity) res; cboOS1.addItem(me.geteCodigo().toString() +
		 * " | " + me.geteDescrip()); cboOS2.addItem(me.geteCodigo().toString()
		 * + " | " + me.geteDescrip()); }
		 * 
		 * Query qMaxPatientNumber = session.getNamedQuery("maxPatientNumber");
		 * List resultList3 = qMaxPatientNumber.list(); Integer maxNumber =
		 * (Integer) resultList3.get(0); System.out.println(maxNumber);
		 */
	}
}
